package com.ptu.spzx.user.Controller;

import com.github.pagehelper.PageInfo;
import com.ptu.spzx.model.dto.h5.UserLoginDto;
import com.ptu.spzx.model.dto.h5.UserRegisterDto;
import com.ptu.spzx.model.dto.system.AssginStuDeptDto;
import com.ptu.spzx.model.entity.user.UserInfo;
import com.ptu.spzx.model.vo.common.Result;
import com.ptu.spzx.model.vo.common.ResultCodeEnum;
import com.ptu.spzx.model.vo.h5.UserInfoVo;
import com.ptu.spzx.user.service.CourseUserService;
import com.ptu.spzx.user.service.UserInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;

import java.util.List;

import org.apache.catalina.User;
import org.simpleframework.xml.Path;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Tag(name = "用户接口")
@RestController
@RequestMapping("/api/user/userInfo")
public class UserInfoController {
    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private CourseUserService courseUserService;

    @Operation(summary = "获取当前登录用户信息")
    @GetMapping("/auth/getCurrentUserInfo")
    public Result<UserInfoVo> getCurrentUserInfo(HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        UserInfoVo userInfoVo = userInfoService.getCurrentUserInfo(token);
        return Result.build(userInfoVo,ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "用户登录")
    @PostMapping("/login")
    public Result login(@RequestBody UserLoginDto userLoginDto){
       String token = userInfoService.login(userLoginDto);
       return Result.build(token,ResultCodeEnum.SUCCESS);
    }

    //注册功能
    @PostMapping("/register")
    public Result register(@RequestBody UserRegisterDto userRegisterDto){
        userInfoService.register(userRegisterDto);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    //添加班级
    @GetMapping("/setClassId/{stuIds}")
    public Result setClassId(@PathVariable("stuIds") List<String> stuIds,@RequestParam(value = "className") String className){
        userInfoService.setClassId(stuIds,className);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    //获取个人详细信息。
    @GetMapping("/getStu/{stuId}")
    public UserInfo getStu(@PathVariable("stuId") String stuId){
        UserInfo userInfo=userInfoService.getStu(stuId);
        return userInfo;
    }

    //获取学生信息列表
    @GetMapping("/getStuList")
    public List<UserInfo> getStuList(@RequestParam("className") String className){
        List<UserInfo> userInfo=userInfoService.getStuList(className);
        return userInfo;
    }
    //为学生分配部门
    @PostMapping("/doAssign")
    public Result doAssign(@RequestBody AssginStuDeptDto assginStuDeptDto){
        userInfoService.doAssignDept(assginStuDeptDto);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    //根据班级名获取学生列表
    @GetMapping("/getStuListByClass")
    public List<String> getStuListByClass(@RequestParam("className") String className){
        List<String> stuIds=userInfoService.getStuListByClass(className);
        return stuIds;
    }

    @GetMapping("/setClassNull")
    public Result setClassNull(@RequestParam("className") String className){
        userInfoService.setClaaNull(className);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }

    //根据班级名获取学生信息
    @GetMapping("/getStuListByClassName/{pageNum}/{pageSize}")
    public PageInfo<UserInfo> getStuListByClassName(@RequestParam("classNames") List<String> classNames
                                                    ,@PathVariable("pageNum") Integer pageNum,
                                                    @PathVariable("pageSize")Integer pageSize){
        PageInfo<UserInfo> stu=userInfoService.getStuListByClassName(classNames,pageNum,pageSize);
        return stu;
    }
    //设置课程教师
    @GetMapping("/addCourseUser")
    public Result addCourseUser(@RequestParam(value = "teachIds") List<Long> teachIds,@RequestParam Long courseId){
        courseUserService.addCourseUser(teachIds,courseId);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @GetMapping("/deleteCourseUser")
    public Result deleteCourseUser(@RequestParam Long curseId){
        courseUserService.deleteCourseUser(curseId);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @GetMapping("/selectCourseIdByteachId")
    public List<Long> selectCourseIdByteachId(@RequestParam Long teachId){
       List<Long> courseIds= courseUserService.selectCourseIdByteachId(teachId);
       return courseIds;
    }

    @GetMapping("/selectTeacherIdByCourseId")
    public List<Long> selectTeacherIdByCourseId(@RequestParam Long courseId){
        List<Long> teacherIds=courseUserService.selectTeacherIdByCourseId(courseId);
        return teacherIds;
    }

    @GetMapping("/getStuListByStuIds")
    public List<UserInfo> getStuListByAllStuId(@RequestParam("stuIds") List<String> stuIds){
        List<UserInfo> userInfos=userInfoService.getStuListByAllClassName(stuIds);
        return userInfos;
    }

}
